package com.retailshop.shopcontrol.common;

import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

public class ControlInterceptor implements HandlerInterceptor {
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {

        String url= request.getRequestURI();
        System.out.println(url);
        if(url=="/")
        {
            System.out.println("A");
            response.sendRedirect("/index.html");
            return false;
        }
        if(url.contains("login") || url.contains("Adminlogin") || url.contains(".js"))
        {
            return true;
        }
        //if(url.contains(".html")) {
            HttpSession httpSession = request.getSession();
            if (httpSession.getAttribute("id") == null) {
                response.sendRedirect("/login.html");
                return false;
            }
            else
            {
                if(url.contains("index")) {
                    int role =Integer.parseInt(httpSession.getAttribute("role").toString());

                    System.out.println(role);

                    if (role == 1) {
                        if(!url.contains("index1"))
                        {
                            if(url.equals("/index.html") || url.equals("/"))
                            {
                                response.sendRedirect("/index1.html");
                            }
                            System.out.println("权限不足");
                            return false;
                        }
                    }
                    else if (role == 2) {
                        if(!url.contains("index2"))
                        {
                            if(url.equals("/index.html"))
                            {
                                response.sendRedirect("/index2.html");
                            }
                            System.out.println("权限不足");
                            return false;
                        }
                    }
                    else if (role == 3) {
                        if(!url.contains("index3"))
                        {
                            if(url.equals("/index.html"))
                            {
                                response.sendRedirect("/index3.html");
                            }
                            System.out.println("权限不足");
                            return false;
                        }
                    }
                    else if (role == 4) {
                        if(!url.contains("index4"))
                        {
                            if(url.equals("/index.html"))
                            {
                                response.sendRedirect("/index4.html");
                            }
                            System.out.println("权限不足");
                            return false;
                        }
                    }
                    else if (role == 5) {
                        if(!url.contains("index5"))
                        {
                            if(url.equals("/index.html"))
                            {
                                response.sendRedirect("/index5.html");
                            }
                            System.out.println("权限不足");
                            return false;
                        }
                    }
                }
            }
        //}
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}
